using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using YayiMgr.Ability.Docking.PatientApp;
using YayiMgr.Ability.Docking.PatientApp.Dto;
using YayiMgr.Domain.BasicData;
using Youshow.Ace.Ability;
using Youshow.Ace.Domain.Repository;

namespace YayiMgr.Ability.PatientApp
{
    public class PatientsServicer: AbilityServicer, IPatientsServicer
    {
        public IRepository<Patients> PatientsRepo { get; set; }
        public async Task<List<PatientsDto>> GetAllPatients()
        {
            List<Patients> patients = await PatientsRepo.GetListAsync();
            var dtos = ModelMapper.Map<List<Patients>,List<PatientsDto>>(patients);
            return dtos;
        }
        public async Task<PatientsDto> GetPatientsById(Guid id)
        {
            Patients patients = await PatientsRepo.GetAsync(id);
            if(patients==null)
                return null;
            var dtos = ModelMapper.Map<Patients,PatientsDto>(patients);
            return dtos;
        }
        public async Task<PatientsDto> AddPatients(PatientsCreateDto input)
        {
            var item = ModelMapper.Map<PatientsCreateDto, Patients>(input);
            item.Id = Guid.NewGuid();
            item.CreateTime = DateTime.Now;
            Patients patients = await PatientsRepo.InsertAsync(item);
            var dtos = ModelMapper.Map<Patients,PatientsDto>(patients);
            return dtos;
        }
        public async Task<PatientsDto> UpdatePatients(Guid id, PatientsUpdateDto input)
        {
            Patients patients = await PatientsRepo.GetAsync(id);
            if(patients==null)
                return null;
            patients = ModelMapper.Map<PatientsUpdateDto, Patients>(input);
            patients = await PatientsRepo.UpdateAsync(patients);
            var dtos = ModelMapper.Map<Patients,PatientsDto>(patients);
            return dtos;
        }
        public async Task DeletePatients(Guid id)
        {
            await PatientsRepo.DeleteAsync(id);
            // Patients patients = await PatientsRepo.GetAsync(id);
            // if(patients!=null)
            //     await PatientsRepo.DeleteAsync(patients);
        }
    }
}